<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class svn_user extends admin {
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('svn_user_model');
	}
	public function vgroup($gids,$g_list){
		$gs = explode(',',$gids);
		$names = array();
		//print_r($g_list);
		if($g_list)foreach($g_list as $v){
			if(in_array($v['id'],$gs)){
				if(!isset($names[$v['sid']])){
					$names[$v['sid']] = array($v['sname']);					
				}
				$names[$v['sid']][] = $v['name'];
			}
		}
		$html = array();
		foreach($names as $v){
			$n = $v[0];
			array_shift($v);
			$html[] = $n.' : '.implode(',',$v);
		}
		//print_r($names);
		return implode('<br/>',$html);
	}
	//svn list
	function init () {
		$infos = array();
			
			$group_db = pc_base::load_model('svn_group_model');
			$g_list = $group_db->query_info('SELECT a.id,a.name,a.sid,ifnull(b.name,\'公共\') sname from v9_svn_group a left join v9_svn b on a.sid=b.id');
			
			/*$group_list = $group_db->array2map($g_list,'id','name');
			$group_list2 = $group_db->array2map($g_list,'sid','sname');
			$group_list3 = $group_db->array2maps($g_list,'sid','id');*/
			
			$where = "";
			$page = $_GET['page'] ? $_GET['page'] : '1';
			$list = $this->db->listinfo($where, $order = 'id DESC', $page, $pagesize = 20);
			$pages = $this->db->pages;
			$show_dialog = true;
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m='.ROUTE_M.'&c='.ROUTE_C.'&a=add\', title:\'添加\', width:\'500\', height:\'300\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加用户');
 			include $this->admin_tpl('svn/user/list');
	}
	
	function add() {
		if(isset($_POST['dosubmit'])) {
			
			$info = $_POST['info'];
			
			$one = $this->db->get_one(array('user'=>$info['user']));
			if(!empty($one))showmessage('用户 名已存在');
			
			$groups = '';
			if(isset($_POST['groups']) && is_array($_POST['groups']))$groups = implode(',', $_POST['groups']);
			$info['groups'] = $groups;
			
			$info['created'] = time();
			$this->db->insert($info);
			$svnServ = pc_base::load_sys_class('svnserv');
			$svnServ->adduser($info);
			//结束
			showmessage(L('operation_success'), '', '', 'add');
		} else {
			
			$group_db = pc_base::load_model('svn_group_model');
			$group_list = $group_db->array2maps($group_db->select($where = '', $data = '*', $limit = '', $order = 'sid asc'),'sid');
			//ksort($group_list);
			$svn_db = pc_base::load_model('svn_model');
			
			$svn_list = $svn_db->select($where = '', $data = 'id,name', $limit = '', $order = 'id asc');
			//array_unshift($svn_list,array('id'=>'0','name'=>'公共'));
			$svn_list = $svn_db->array2map($svn_list,'id','name');
			//array_unshift($svn_list,'公共');
			$svn_list[0] = '公共';
			///print_r($svn_list)
			include $this->admin_tpl('svn/user/add');
		}
	}
	function delete() {
		$id = intval($_GET['id']);
		$one = $this->db->get_one(array('id'=>$id));
		//print_r($one);exit();
		if($one){
			$this->db->delete(array('id'=>$id));
			$svnServ = pc_base::load_sys_class('svnserv');
			$svnServ->adduser($one);
		}
		showmessage(L('operation_success'));
	}
	
	function edit() {
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['id']);
			
			$info = $_POST['info'];
			$one = $this->db->get_one("user='".$info['user']."' AND id<>".$id);
			if(!empty($one))showmessage('用户 名已存在');
			
			$groups = '';
			if(isset($_POST['groups']) && is_array($_POST['groups']))$groups = implode(',', $_POST['groups']);
			$info['groups'] = $groups;
			
			$old = $this->db->get_one(array('id'=>$id));
			if($old['pwd']!=$info['pwd']){
				$svnServ = pc_base::load_sys_class('svnserv');
				$svnServ->adduser($info);
			}
			
			$this->db->update($info,array('id'=>$id));
			
			//结束语言文件修改
			showmessage(L('operation_success'), '', '', 'edit');
		} else {
			
			$group_db = pc_base::load_model('svn_group_model');
			$group_list = $group_db->array2maps($group_db->select($where = '', $data = '*', $limit = '', $order = 'sid asc'),'sid');
			
			$svn_db = pc_base::load_model('svn_model');
			$svn_list = $svn_db->array2map($svn_db->select($where = '', $data = 'id,name', $limit = '', $order = 'id asc'),'id','name');
			$svn_list[0] = '公共';
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id));
			if($r) extract($r);
			$a_gids = explode(',', $r['groups']);
			include $this->admin_tpl('svn/user/edit');
		}
	}
	
	
}